<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>1、找到数组中出现最频繁的偶数</title>
</head>

<body>
  <script>
    // let arr = [1, 2, 2, 2, 3, 5, 4, 6, 6]
    // let num = []
    // let sum = arr.forEach((a) => {
    //   if (a % 2 == 0) {
    //     num.push(a)
    //   }
    // })
    // console.log(num)

    // let objs = []
    // num.forEach(el => {
    //   objs.forEach(oE => {
    //     if (oE.value === el) {
    //       oE.i++
    //     }
    //   })
    //   let obj = objs.find(objE => objE.value === el)
    //   if (!obj) {
    //     objs.push({
    //       value: el,
    //       i: 1
    //     })
    //   }
    // })
    // console.log(objs);




    var array = function (nums) {
      let ans = -1;
      let times = -1;
      const map = {};

      nums.sort((a, b) => a - b);
      for (const val of nums) {
        if (val % 2 === 0) {
          map[val] = (map[val] || 0) + 1;

          if (map[val] > times) {
            ans = val;
            times = map[val];
          }
        }
      }

      return ans;
    }
    console.log(array([1, 2, 3, 2, 5, 4, 6, 4]))
  </script>
</body>

</html>